import http from '@/api/http.js'
const weixinPay = function(orderData,successFun='',failFun = '') {
	let ordersn = orderData.ordersn,
		pageType = orderData.pageType ? orderData.pageType : 0; // 页面跳转模式 默认0为不跳转 1为关闭当前页面跳转
	let params = {
		method: 'api/v2/payment/wechatapp/get_pay_data',
		ordersn
	};
	uni.showLoading({
		title:'正在发起支付'
	});
	http(params).then(res => {
		uni.hideLoading();
		if (res.status) {
			let data = res.data;
			//是否已支付
			if(data.is_pay == 1) {
				uni.redirectTo({
					url: `/pages/order/paysuccess?ordersn=${data.info.ordersn}&orderid=${data.info.id}`
				})
				return;
			}
			uni.requestPayment({
				'timeStamp': data.timeStamp,
				'nonceStr': data.nonce_str,
				'package': data.package,
				'signType': 'MD5',
				'paySign': data.paySign,
				'success': function(res) {
					uni.showToast({
						title: "支付成功",
						icon: 'none',
						duration: 2000
					});
					// 成功执行事件
					if(successFun)
					{
						setTimeout(()=>{
							successFun();
						},2000);
					}
				},
				'fail': function(data) {
					uni.showToast({
						title: "支付已取消",
						icon: 'none',
						duration: 2000
					});
					// 失败执行事件
					if(failFun && pageType )
					{
						setTimeout(()=>{
							failFun();
						},2000);
					}
				}
			})
		} else {
			uni.showToast({
				title: res.msg,
				icon: 'none'
			});
			
		}
	});
}

export default weixinPay
